package org.edu.modules.jxgz.controller;

import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.jxgz.api.CommonApi;
import org.edu.modules.jxgz.config.ConfigConstant;
import org.edu.modules.jxgz.entity.VeJwBjgg;
import org.edu.modules.jxgz.entity.VeJwJxrz;
import org.edu.modules.jxgz.entity.VeJwText;
import org.edu.modules.jxgz.service.IVeJwBjggService;
import org.edu.modules.jxgz.util.VeJwTextUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: ve_jw_bjgg
 * @Author: jeecg-boot
 * @Date:   2021-04-27
 * @Version: V1.0
 */
@Api(tags="ve_jw_bjgg")
@RestController
@RequestMapping("/jxgz/veJwBjgg")
@Slf4j
public class VeJwBjggController extends BaseController<VeJwBjgg, IVeJwBjggService> {
	@Autowired
	private IVeJwBjggService veJwBjggService;
	 @Autowired
	 private CommonApi commonApi;

	/**
	 * 分页列表查询
	 *
	 * @param veJwBjgg
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ve_jw_bjgg-分页列表查询")
	@ApiOperation(value="ve_jw_bjgg-分页列表查询", notes="ve_jw_bjgg-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(VeJwBjgg veJwBjgg,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<VeJwBjgg> queryWrapper = QueryGenerator.initQueryWrapper(veJwBjgg, req.getParameterMap());
		Page<VeJwBjgg> page = new Page<VeJwBjgg>(pageNo, pageSize);
		IPage<VeJwBjgg> pageList = veJwBjggService.page(page, queryWrapper);
		for (VeJwBjgg temp : pageList.getRecords()) {
			if(StringUtils.isNotBlank(temp.getContenttid())){
				VeJwText veJwText = VeJwTextUtils.getVeJwTextById(Integer.valueOf(temp.getContenttid()));
				if (veJwText != null) {
					temp.setContenttidText(veJwText.getText());
				}
			}

			if(StringUtils.isNotBlank(temp.getVerifytid())){
				VeJwText veJwText = VeJwTextUtils.getVeJwTextById(Integer.valueOf(temp.getVerifytid()));
				if(veJwText !=null){
					temp.setVerifytidText(veJwText.getText());
				}
			}
		}
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param veJwBjgg
	 * @return
	 */
	@AutoLog(value = "ve_jw_bjgg-添加")
	@ApiOperation(value="ve_jw_bjgg-添加", notes="ve_jw_bjgg-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VeJwBjgg veJwBjgg) {
		String rznrtidText = veJwBjgg.getContenttidText();
		if (StringUtils.isBlank(rznrtidText)) {
			return Result.error("内容不可为空");
		}
		try {
			Result<JSONObject> jsonObjectResult = commonApi.queryAppUserByUserId(CommonApi.COMMON_TOKEN_KEY, veJwBjgg.getAdduser());
			if (jsonObjectResult == null || jsonObjectResult.getResult() != null) {
				JSONObject result = jsonObjectResult.getResult();
				veJwBjgg.setAddusername(result.getString("userName"));
			}else {
				return Result.error("用户信息获取失败");
			}
		} catch (Exception e) {
			return Result.error("用户信息获取失败");
		}
		//获取班级信息
		try {
			Result<JSONObject> jsonObjectResult = commonApi.queryBanJiById(CommonApi.COMMON_TOKEN_KEY, veJwBjgg.getBjid());
			if (jsonObjectResult == null || jsonObjectResult.getResult() != null) {
				JSONObject result = jsonObjectResult.getResult();
				veJwBjgg.setBjmc(result.getString("xzbmc"));
			}else {
				return Result.error("班级信息获取失败");
			}
		} catch (Exception e) {
			return Result.error("班级信息获取失败");
		}
		veJwBjgg.setVerifyusername("");
		veJwBjgg.setContenttid(VeJwTextUtils.add(rznrtidText)+"");
		veJwBjggService.save(veJwBjgg);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param veJwBjgg
	 * @return
	 */
	@AutoLog(value = "ve_jw_bjgg-编辑")
	@ApiOperation(value="ve_jw_bjgg-编辑", notes="ve_jw_bjgg-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody VeJwBjgg veJwBjgg) {
		String rznrtidText = veJwBjgg.getContenttidText();
		if (StringUtils.isBlank(rznrtidText)) {
			return Result.error("内容不可为空");
		}
		//获取班级信息
		try {
			Result<JSONObject> jsonObjectResult = commonApi.queryBanJiById(CommonApi.COMMON_TOKEN_KEY, veJwBjgg.getBjid());
			if (jsonObjectResult == null || jsonObjectResult.getResult() != null) {
				JSONObject result = jsonObjectResult.getResult();
				veJwBjgg.setBjmc(result.getString("xzbmc"));
			}else {
				return Result.error("班级信息获取失败");
			}
		} catch (Exception e) {
			return Result.error("班级信息获取失败");
		}
		veJwBjgg.setContenttid(VeJwTextUtils.addOrUpdate(Integer.valueOf(veJwBjgg.getContenttid()),rznrtidText)+"");
		veJwBjggService.updateById(veJwBjgg);
		veJwBjggService.initVerifyt(veJwBjgg);
		return Result.ok("编辑成功!");
	}

	 /**
	  *  评审
	  *
	  * @param veJwBjgg
	  * @return
	  */
	 @AutoLog(value = "ve_jw_bjgg-评审")
	 @ApiOperation(value="ve_jw_bjgg-评审", notes="ve_jw_bjgg-评审")
	 @PutMapping(value = "/audit")
	 public Result<?> audit(@RequestBody VeJwBjgg veJwBjgg) {
		 try {
			 Result<JSONObject> jsonObjectResult = commonApi.queryAppUserByUserId(CommonApi.COMMON_TOKEN_KEY, veJwBjgg.getVerifyuser());
			 if (jsonObjectResult == null || jsonObjectResult.getResult() != null) {
				 JSONObject result = jsonObjectResult.getResult();
				 veJwBjgg.setVerifyusername(result.getString("userName"));
			 }else {
				 return Result.error("用户信息获取失败");
			 }
		 } catch (Exception e) {
			 return Result.error("用户信息获取失败");
		 }
		 if( StringUtils.isNotBlank(veJwBjgg.getVerifytidText())){
			 veJwBjgg.setVerifytid(VeJwTextUtils.addOrUpdate(Integer.valueOf(veJwBjgg.getVerifytid()),veJwBjgg.getVerifytidText())+"");
		 }
		 veJwBjgg.setVerifytime(new Date());
		 veJwBjggService.updateById(veJwBjgg);
		 return Result.ok("评审成功!");
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ve_jw_bjgg-通过id删除")
	@ApiOperation(value="ve_jw_bjgg-通过id删除", notes="ve_jw_bjgg-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		veJwBjggService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ve_jw_bjgg-批量删除")
	@ApiOperation(value="ve_jw_bjgg-批量删除", notes="ve_jw_bjgg-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.veJwBjggService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ve_jw_bjgg-通过id查询")
	@ApiOperation(value="ve_jw_bjgg-通过id查询", notes="ve_jw_bjgg-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VeJwBjgg veJwBjgg = veJwBjggService.getById(id);
		if(veJwBjgg==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(veJwBjgg);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param veJwBjgg
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwBjgg veJwBjgg) {
        return super.exportXls(request, veJwBjgg, VeJwBjgg.class, "ve_jw_bjgg");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwBjgg.class);
    }

}
